<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules" :label-col="labelCol" :wrapper-col="wrapperCol">
        <!-- 第一行：客户信息 -->
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="客户名称" prop="customerName">
              <a-input v-model="model.customerName" placeholder="请输入客户名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="客户电话" prop="customerPhone">
              <a-input v-model="model.customerPhone" placeholder="请输入客户电话"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>

        <!-- 第二行：物流信息和门类型 -->
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="物流信息" prop="logisticsType">
              <a-select v-model="model.logisticsType" placeholder="请选择物流信息">
                <a-select-option :value="0">到付</a-select-option>
                <a-select-option :value="1">寄付</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="门类型" prop="type">
              <a-select v-model="model.type" placeholder="请选择门类型">
                <a-select-option :value="0">推拉</a-select-option>
                <a-select-option :value="1">平开</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>

        <!-- 第三行：收货地址 -->
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="收货地址" prop="deliveryAddress">
              <a-input v-model="model.deliveryAddress" placeholder="请输入收货地址"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>

        <!-- 第四行：备注 -->
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="备注" prop="remark">
              <a-textarea v-model="model.remark" :rows="3" placeholder="请输入备注"></a-textarea>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'InquiryModal',
  components: {},
  data() {
    return {
      title: '操作',
      visible: false,
      model: {
        type: 0,
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },

      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        customerName: [{ required: true, message: '请输入客户名称!', trigger: 'blur' }],
        customerPhone: [{ required: true, message: '请输入客户电话!', trigger: 'blur' }],
        deliveryAddress: [{ required: true, message: '请输入收货地址!', trigger: 'blur' }],
        logisticsType: [{ required: true, message: '请选择物流信息!', trigger: 'change' }],
        type: [{ required: true, message: '请选择门类型!', trigger: 'change' }],
      },
      url: {
        add: '/oms/inquiry/add',
        edit: '/oms/inquiry/edit',
        queryById: '/oms/inquiry/queryById',
      },
    }
  },
  methods: {
    add() {
      this.edit({ status: '0' })
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign(
        {
          type: 0,
        },
        record
      )
      // 设置默认值
      if (!this.model.id) {
        this.model.logisticsType = 0
        this.model.status = '0'
        this.model.type = 0
      }
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(this.model)
      })

      if (record.id) {
        this.title = '编辑询单基本信息'
      } else {
        this.title = '新增询单基本信息'
      }
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }

          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
                that.visible = false
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
    handleCancel() {
      this.visible = false
    },
  },
}
</script>
